<?php

/**
 * uniBoard Online List
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-20 21:23:27 +0000 (Mon, 20 Sep 2010) $
 * $Revision: 64 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_online{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Base URL for paginator
	 *
	 * @var string
	 */
	
	private $base_url 		= '';
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Load Language Files
		$appCore -> loadLanguageFile('board_index');
		$appCore -> loadLanguageFile('users');
		
		// Can we see users list?
		if ( !$appCore -> conf['sessions_allow_browse'])
		{
			// Set Error
			$appCore -> output -> setEvent( $appCore -> lang['action_not_allowed_disabled'], $appCore -> lang['users_online_list'], 0);
			
			// Draw Events
			$appCore -> output -> drawEvents();
		}
		else if ( $appCore -> user_acl['can_browse_sessions'] == 0)
		{
			// Set Error
			$appCore -> output -> setEvent( $appCore -> lang['action_not_allowed_access'], $appCore -> lang['users_online_list'], 0);
			
			// Draw Events
			$appCore -> output -> drawEvents();
		}
		else
		{	
			// Load additional lang file
			$appCore -> loadLanguageFile('board_localisations');
			
			// Load List Templates
			$templates = new tpl_users_list( $appCore);
			
			// Set basic URL
			$this -> base_url = $appCore -> niceLink('act=online&st=%s', $appCore -> lang['users_online_list']);
				
			// Search query
			$search_sql = '';
				
			// Display mode
			if ( strlen($appCore -> _REQ('dm')) > 0)
			{
				// Add to link
				$this -> base_url .= '&dm=' . ((int) $appCore -> _REQ('dm'));
				
				// Add to sql
				switch ($appCore -> _REQ('dm'))
				{
					case 1:
						$search_sql .= 'WHERE s.user_id > \'0\'';
					break;
					case 2:
						$search_sql .= 'WHERE s.user_id = \'-1\' AND s.crawler_id > \'0\'';
					break;
					case 3:
						$search_sql .= 'WHERE s.user_id = \'-1\'';
					break;
					default:
						$search_sql .= '';
					break;
				}
				
			}
			
			// Template Variables
			$tpl_vars = array(
				'found_users'	=> $this -> appCore -> db -> count( 'sessions s ' . $search_sql),
				'users_list' 	=> ''
			);
			
			// Users groups
			$users_groups = $appCore -> users -> getUserGroups( false);
			
			// Get Ordering thing
			$order_dir = (bool) $appCore -> _REQ('od');
			$order_type = ((int) $appCore -> _REQ('om') >= 0 && (int) $appCore -> _REQ('om') <= 2 ? (int) $appCore -> _REQ('om') : 0 );
			
			// Set URL
			$this -> base_url .= '&od=' . $order_dir . '&om=' . $order_type;
			
			// Anything found?
			if ( $tpl_vars['found_users'] > 0)
			{
				// Start Paginator
				$paginator = new class_paginator($appCore, $this -> base_url, $tpl_vars['found_users'], 20);
				
				// Draw It
				$tpl_vars['paginator'] = $paginator -> draw();
						
				// Fix order type
				switch ($order_type)
				{
					case 1:
						$order_type = 'u.user_name';
					break;
										
					default:
						$order_type = 's.session_last_active';
					break;
				}
					
				// Select users
				$appCore -> db -> query('SELECT s.user_id, s.crawler_id, s.session_start, s.session_last_active, s.session_ip, s.session_agent, s.session_action, s.session_forum, s.session_topic, s.session_user,
				u.user_name, u.user_avatar_type, u.user_avatar_image, u.user_avatar_width, u.user_avatar_height,
				c.crawler_name,
				su.user_name as session_user_name
				FROM ' . DB_PREFIX . 'sessions s
				LEFT JOIN ' . DB_PREFIX . 'users u ON s.user_id = u.user_id
				LEFT JOIN ' . DB_PREFIX . 'crawlers c ON s.crawler_id = c.crawler_id
				LEFT JOIN ' . DB_PREFIX . 'users su ON s.session_user = su.user_id
				' . $search_sql . '
				ORDER BY s.session_last_active' . ($order_dir ? ' DESC' : ' ASC') . '
				LIMIT ' . $paginator -> getPosition() . ',20');
				
				// Draw them
				while ( $session_result = $appCore -> db -> fetch_assoc())
				{
					// Draw Row
					$tpl_vars['users_list'] .= $templates -> tpl_online_list_item(array(
						'full_access' 			=> $appCore -> user_acl['can_browse_sessions'] == 2,
						'session_ip'			=> $session_result['session_ip'],
						'session_agent'			=> htmlspecialchars($session_result['session_agent']),
						'session_last_active'	=> $session_result['session_last_active'],
						'session_location'		=> $this -> getLocation($session_result['session_action'], $session_result['session_forum'], '', $session_result['session_topic'], '', $session_result['session_user'], $session_result['session_user_name']),
						'user_id' 				=> $session_result['user_id'],
						'user_name' 			=> ($session_result['user_id'] > 0 ? htmlspecialchars($session_result['user_name']) : ($session_result['crawler_id'] > 0 ? htmlspecialchars($session_result['crawler_name']) : $appCore -> lang['user_guest'])),
						'user_avatar_type' 		=> ($appCore -> user['id'] == $session_result['user_id'] || $appCore -> user_acl['can_see_users_avatars'] ? $session_result['user_avatar_type'] : 0),
						'user_avatar_image'		=> class_users::avatarLocation($session_result['user_avatar_type']) . htmlspecialchars($session_result['user_avatar_image']),
						'user_avatar_width' 	=> $session_result['user_avatar_width'],
						'user_avatar_height' 	=> $session_result['user_avatar_height'],
					));
				}
				
				// Free result
				$appCore -> db -> freeResult();
			}
			else
			{
				// Set Error
				$appCore -> output -> setEvent( $appCore -> lang['user_list_search_empty'], $appCore -> lang['users_list_search_results'], 0);
			}
				
			// Fill search form
			$tpl_vars['filter_list'] = $appCore -> drawRawList(array(
				0 => $appCore -> lang['users_online_list_search_0'],
				1 => $appCore -> lang['users_online_list_search_1'],
				2 => $appCore -> lang['users_online_list_search_2'],
				3 => $appCore -> lang['users_online_list_search_3']
			), $appCore -> _REQ('dm'));
			
			// Sorting ops
			$tpl_vars['results_sort_direction'] = $appCore -> drawRawList(array(
				0 => $appCore -> lang['users_list_search_sort_dir_0'],
				1 => $appCore -> lang['users_list_search_sort_dir_1'],
			), $appCore -> _REQ('od'));
			
			$tpl_vars['results_sort_method'] = $appCore -> drawRawList(array(
				0 => $appCore -> lang['users_online_list_search_sort_0'],
				1 => $appCore -> lang['users_list_search_sort_0'],
			), $appCore -> _REQ('om'));
				
			// Draw output
			$appCore -> output -> draw( $appCore -> lang['users_online_list'], array(
				array(
					$appCore -> niceLink('act=online',$appCore -> lang['users_online_list']),
					$appCore -> lang['users_online_list']
				)
			), $templates -> tpl_online_list( $tpl_vars));
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Returns user-understandable session location
	 *
	 * @param string $action
	 * @param integer $forum_id
	 * @param string $forum_name
	 * @param integer $topic_id
	 * @param string $topic_name
	 * @param integer $user_id
	 * @param string $user_name
	 * @return string
	 */
	
	function getLocation( $action, $forum_id, $forum_name, $topic_id, $topic_name, $user_id, $user_name){
		
		// Switch action
		switch ($action)
		{
			case 'user':
				// Return User Profile or Index
				return ($user_id > 0 && isset($user_name[0])? sprintf($this -> appCore -> lang['board_location_user'], '<a href="' . $this -> appCore -> niceLink('act=user&user=' . $user_id, $user_name) . '">' . htmlspecialchars($user_name) . '</a>') : $this -> appCore -> lang['board_location_user']);
			break;
			
			default:
				// Return Board Index
				return $this -> appCore -> lang['board_location_index'];
			break;
		}
		
	}
	
}